class OrdersController < ApplicationController    
  
  def search
  end
  
  def index
    params[:sql_key_word] = "%#{params[:key_words]}%"
    @orders = Order.where(['orders.order_no like :sql_key_word or buyers.full_name like :sql_key_word or buyers.mobile like :sql_key_word or buyers.link_phone like :sql_key_word or buyers.address like :sql_key_word ', params]).includes(:buyer)
  end
  
  def show
    @order = Order.find(params[:id])
    @buyer = @order.buyer
  end
  
  def create
    @order = Order.new
    @order.save!
    redirect_to edit_order_url(@order)
  end
  
  def edit 
    @order = Order.find(params[:id])
    @buyer = @order.buyer
    @order.edited!
    respond_to do |format|
      format.html do
      end
      format.js do 
      end
    end
  end
  
  def update 
    @order = Order.find(params[:id])
    render :edit if not @order.update_attributes params[:order]        
  end
  
  def destroy
    @order = Order.find(params[:id])
    @order.blanked_out!
    respond_to do |format|
      format.html do
        redirect_to order_url(@order)
      end
      format.js do 
      end
    end
  end
  
  def effected
    @order = Order.find(params[:id])
    @order.effected!
    redirect_to order_url(@order)
  end
  
  def show_buyer
    @buyer = Buyer.find(params[:id]) if params[:id] != 'none'
  end
  
  def new_buyer
    @buyer = Buyer.new
  end
  
  def create_buyer
    @buyer = Buyer.find_or_create_by_full_name_and_mobile(params[:buyer][:full_name],params[:buyer][:mobile])
    if @buyer.update_attributes(params[:buyer])            
      @order = Order.find(params[:id])
      @order.update_attribute :buyer_id, @buyer.id
    else
      render :edit_buyer
    end   
  end
  
  def edit_buyer
    @buyer = Buyer.find(params[:id])
  end
  
  def update_buyer
    @buyer = Buyer.find(params[:id])
    render :edit_buyer if not @buyer.update_attributes params[:buyer]
  end
  
  def stored_furnitures
    @order = Order.find(params[:id])
    @stats = FurnitureLogisticStat.where(['furnitures.furniture_style_id = :furniture_style_id and furnitures.material_id = :material_id', params]).includes(:furniture)    
    spots_orderedable_count
  end
  
  def new_spots_ordered_order_item
    @order_item = OrderItem.new :order_id => params[:id]
  end

  def new_order_made_order_item
    @order_item = OrderItem.new :order_id => params[:id]
  end

  def create_spots_ordered_order_item
    # spots_ordered
    params[:order_item][:ordered_count] = params[:ordered_count].inject(0) { |sum, count| sum + count.to_i }
    @order_item = OrderItem::SpotsOrdered.new params[:order_item]
    if not @order_item.save
      @stats = FurnitureLogisticStat.find(params[:stat_id]) if params[:stat_id].present?
      render :new_order_item
    else
      @order = @order_item.order
      # stat_order_items
      params[:stat_id].each_with_index do |stat_id,index|
        @order_item.add_stat_order_item(
          FurnitureLogisticStatOrderItem.new(
            :furniture_logistic_stat_id => stat_id,
            :order_item_id => @order_item.id,
            :ordered_count => params[:ordered_count][index]
          )
        )
      end
    end
  end
  
  def create_order_made_order_item
    # order_made
    params[:order_item][:furniture_id] = Furniture.find_or_create_by_furniture_style_id_and_material_id_and_spec(params[:furniture_style_id],params[:material_id],params[:furniture_spec]).id
    @order_item = OrderItem::FactoryOrderMade.new params[:order_item]
    @order = @order_item.order
    if not @order_item.save
      render :new_order_made_order_item
    end
  end

  def edit_spots_ordered_order_item
    @order_item = OrderItem.find(params[:id])
    params[:furniture_style_id] = @order_item.furnitures.first.furniture_style_id
      params[:material_id] = @order_item.furnitures.first.material_id
      @stats = @order_item.furniture_logistic_stats
      FurnitureLogisticStat.where(['furnitures.furniture_style_id = ? and furnitures.material_id = ?',@order_item.furnitures.first.furniture_style_id,@order_item.furnitures.first.material_id]).includes(:furniture).each do |stat|
        @stats << stat if not @stats.include?(stat)
      end
      spots_orderedable_count
  end
  
  def edit_order_made_order_item
    @order_item = OrderItem.find(params[:id])
  end

  def update_spots_ordered_order_item
    @order_item = OrderItem.find(params[:id])
    @order = @order_item.order
    #spots_ordered
    if not @order_item.update_attributes params[:order_item]
      render :edit_order_item
    else
      # udpate stat_order_items
      params[:stat_order_item_id].each_with_index do |id,index|
        if id.blank?
          @order_item.add_stat_order_item(
          FurnitureLogisticStatOrderItem.new(
              :furniture_logistic_stat_id => params[:stat_id][index],
              :order_item_id => @order_item.id,
              :ordered_count => params[:ordered_count][index]
            )
          )
        else
          stat_order_item = FurnitureLogisticStatOrderItem.find(id)
          if params[:ordered_count][index].to_i > 0
            stat_order_item.update_attributes :ordered_count => params[:ordered_count][index]
          else
            stat_order_item.destroy
          end
        end
      end
    end
  end
  
  def update_order_made_order_item
    @order_item = OrderItem.find(params[:id])
    @order = @order_item.order
    #order_made
    if not @order_item.update_attributes params[:order_item]
      render :edit_order_item
    else
      @order_item.furniture.update_attributes :spec => params[:furniture_spec]
    end
  end
  
  def order_items
    @order = Order.find(params[:id])
  end
  
  def destroy_order_item
    @order_item = OrderItem.find(params[:id])
    @order_item.destroy
  end
  
  private 
  def spots_orderedable_count
    @spots_orderedable_count = @stats.each_with_object([]) do |stat,counts|
      counts << stat.spots_orderedable_count
    end.sum
  end
end
